package lapsnake.model.misc;

import lapsnake.model.*;
import lapsnake.model.data.*;
import lapsnake.py.ast.ASTHelperFactory; 
import lapsnake.ui.JTreeLabel;

import java.awt.Component;
import java.beans.PropertyVetoException;

import org.python.antlr.ast.ExceptHandler;

/**Wrapper class with helper functions for excepthandler nodes*/
public class LapExcepthandler extends LapNode {
	
	public final OptionalChild<LapExpression> type = new OptionalChild<>(this, LapExpression.class);
	public final OptionalChild<LapExpression> name = new OptionalChild<>(this, LapExpression.class);
	@NewRow public final ChildList<LapStatement> body = new ChildList<>(this, LapStatement.class, false);
	
	public LapExcepthandler(PyAST pyAST, ChildField<? super LapExcepthandler> parent, ExceptHandler node) throws PropertyVetoException {
		super(pyAST, parent, node);
		
		ASTHelperFactory.makeHelper(pyAST, type, node.getInternalType());
		ASTHelperFactory.makeHelper(pyAST, name, node.getInternalName());
		ASTHelperFactory.makeHelpers(pyAST, body, node.getInternalBody());
		
		buildTextNodes(node);
	}
	@Override
	protected Component getFillerComponent(int childIndex,
		ChildField<?> childField)
	{
		if(childField == type)
		{
			if(type.isEmpty())
				return new JTreeLabel("Any exception", false);
			else
				return new JTreeLabel("Type", false);
		}
		else if(childField == name)
		{
			if(name.isEmpty())
				return null;
			else
				return new JTreeLabel("as", false);
		}
		else
			return new JTreeLabel("Do:", true);
	}
}
